﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class MenuByUserRoles : ClassManager.SessionManagerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void lbClearUserRolesFilters_Click(object sender, EventArgs e)
        {
            UserRolesGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in UserRolesGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            UserRolesGrid.MasterTableView.Rebind();
            Build_Menu(getRoleIdSelected());
        }

        private void ResizeGrid()
        {
            decimal Height = Convert.ToDecimal(HiddenFieldHeight.Value) ;
            decimal Width = Convert.ToDecimal(HiddenFieldWidth.Value)/3;

            UserRolesGrid.Height = Unit.Parse(Convert.ToString(Height));
            UserRolesGrid.Width = Unit.Parse(Convert.ToString(Width));
            AllMenuGrid.Height = Unit.Parse(Convert.ToString(Height));
            AllMenuGrid.Width = Unit.Parse(Convert.ToString(Width));
            MenuGrid.Height = Unit.Parse(Convert.ToString(Height));
            //MenuGrid.Width = Unit.Parse(Convert.ToString(Width));

        }
       

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            ShowErrorMessage(HiddenFieldError.Value);
        }

        public void ShowErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Error", "objWindowsMessageManager.show_Error('" + msg + "','Roles ...');", true);
        }

        public void ShowInfoMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Info", "objWindowsMessageManager.show_Info('" + msg + "','Roles ...');", true);
        }

        public void ShowWarningMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Warning", "objWindowsMessageManager.show_Warning('" + msg + "','Roles ...');", true);
        }

        protected void UserRolesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            UserRolesGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.UserRolesProvider.GetAllByCompanyID(getSessionManager().CompanyIDSelectedComboBox);

            ResizeGrid();
        }

        protected void MenuGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            MenuGrid.DataSource = new String[]{"Tree"};
        }

        protected void uscMenuItemsSearch_OnCancelEvent()
        {
            ResizeGrid();
        }
        
        protected void UserRolesGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            int roleId = getRoleIdSelected();

            Build_Menu(roleId);
            BuildAllMenu();
            ResizeGrid();
        }

        protected int getRoleIdSelected() {
            try {
                return Convert.ToInt32(UserRolesGrid.Items[UserRolesGrid.SelectedItems[0].ItemIndex]["UserRolesID"].Text);            
            }
            catch{ }
            return -1;
        }

        private void Build_Menu(int roleId)
        {

            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.Menu> menuEntities = ConvertDataTableToEntities(GPSTrackingDBV2Custom.Data.DataRepository.MenuByUserRoleProvider.GetAllByUserRoleID(roleId, getSessionManager().CompanyIDSelectedComboBox));

            Telerik.Web.UI.RadTreeView tree = MenuGrid.Items[0].FindControl("MenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
            tree.Nodes.Clear();
            Telerik.Web.UI.RadTreeNode menuItemNivel1 = new Telerik.Web.UI.RadTreeNode();
            menuItemNivel1.Text = "Módulo Manager";
            tree.Nodes.Add(menuItemNivel1);

            var menuByUserRolesParentEntities = menuEntities.Where(x => x.Parent == null && x.MenuTypeId == 1 && x.IsVisibleUser).OrderBy(o => o.BySequence);
            foreach (GPSTrackingDBV2.Entities.Menu row in menuByUserRolesParentEntities)
            {
                Telerik.Web.UI.RadTreeNode menuItem = new Telerik.Web.UI.RadTreeNode();
                menuItem.ImageUrl = "../../Template/Icons/16px/" + row.ImageUrl;                                
                menuItem.Text = row.Name;
                menuItem.ToolTip = row.ToolTip;
                menuItem.Value = row.MenuId.ToString();

                var menuByUserRolesChildEntities = menuEntities.Where(x => x.Parent == row.MenuId && x.MenuTypeId == 1 && x.IsVisibleUser).OrderBy(o => o.BySequence);

                foreach (GPSTrackingDBV2.Entities.Menu rowChild in menuByUserRolesChildEntities)
                {
                    Telerik.Web.UI.RadTreeNode menuItemChild = new Telerik.Web.UI.RadTreeNode();
                    menuItemChild.ImageUrl = "../../Template/Icons/16px/" + rowChild.ImageUrl;                    
                    menuItemChild.Text = rowChild.Name;
                    menuItemChild.ToolTip = rowChild.ToolTip;
                    menuItemChild.Value = Convert.ToString(rowChild.MenuId);
                    
                    menuItem.Nodes.Add(menuItemChild);
                }

                tree.Nodes[0].Nodes.Add(menuItem);
            }

            Telerik.Web.UI.RadTreeView tree2 = MenuGrid.Items[0].FindControl("MenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
            tree2.Nodes.Clear();
            Telerik.Web.UI.RadTreeNode menuItemNivel2 = new Telerik.Web.UI.RadTreeNode();
            menuItemNivel2.Text = "Módulo Mapa";
            tree2.Nodes.Add(menuItemNivel2);



            var menuByUserRolesParentEntitiesMap = menuEntities.Where(x => x.Parent == null && x.MenuTypeId == 2 && x.IsVisibleUser).OrderBy(o => o.BySequence);
            foreach (GPSTrackingDBV2.Entities.Menu row in menuByUserRolesParentEntitiesMap)
            {
                Telerik.Web.UI.RadTreeNode menuItem = new Telerik.Web.UI.RadTreeNode();
                menuItem.ImageUrl = "../../Template/Icons/16px/" + row.ImageUrl;
                menuItem.Text = row.Name;
                menuItem.ToolTip = row.ToolTip;
                menuItem.Value = row.MenuId.ToString();

                var menuByUserRolesChildEntities = menuEntities.Where(x => x.Parent == row.MenuId && x.MenuTypeId == 2 && x.IsVisibleUser).OrderBy(o => o.BySequence);

                foreach (GPSTrackingDBV2.Entities.Menu rowChild in menuByUserRolesChildEntities)
                {
                    Telerik.Web.UI.RadTreeNode menuItemChild = new Telerik.Web.UI.RadTreeNode();
                    menuItemChild.ImageUrl = "../../Template/Icons/16px/" + rowChild.ImageUrl;
                    menuItemChild.Text = rowChild.Name;
                    menuItemChild.ToolTip = rowChild.ToolTip;
                    menuItemChild.Value = Convert.ToString(rowChild.MenuId);

                    menuItem.Nodes.Add(menuItemChild);
                }

                tree2.Nodes[0].Nodes.Add(menuItem);
            }

            Telerik.Web.UI.RadTreeView tree3 = MenuGrid.Items[0].FindControl("MenuRadMobileTreeView") as Telerik.Web.UI.RadTreeView;
            tree3.Nodes.Clear();
            Telerik.Web.UI.RadTreeNode menuItemNivel3 = new Telerik.Web.UI.RadTreeNode();
            menuItemNivel3.Text = "Módulo Dispositivos Móviles";
            tree3.Nodes.Add(menuItemNivel3);



            var menuByUserRolesParentEntitiesMobile = menuEntities.Where(x => x.Parent == null && x.MenuTypeId == 3).OrderBy(o => o.BySequence);
            foreach (GPSTrackingDBV2.Entities.Menu row in menuByUserRolesParentEntitiesMobile)
            {
                Telerik.Web.UI.RadTreeNode menuItem = new Telerik.Web.UI.RadTreeNode();
                menuItem.ImageUrl = "../../Template/Icons/16px/pda-icon.png";
                menuItem.Text = row.Name;
                menuItem.ToolTip = row.ToolTip;
                menuItem.Value = row.MenuId.ToString();

                tree3.Nodes[0].Nodes.Add(menuItem);
            }

            tree.ExpandAllNodes();
            tree2.ExpandAllNodes();
            tree3.ExpandAllNodes();
        }

        protected GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.Menu> ConvertDataTableToEntities(System.Data.DataTable dt)
        {
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.Menu> entities = new GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.Menu>();
            foreach (System.Data.DataRow row in dt.Rows)
            {
                GPSTrackingDBV2.Entities.Menu entity = new GPSTrackingDBV2.Entities.Menu();

                entity.MenuId = Convert.ToInt32(row[GPSTrackingDBV2.Entities.MenuColumn.MenuId.ToString()]);
                entity.Name = Convert.ToString(row[GPSTrackingDBV2.Entities.MenuColumn.Name.ToString()]);
                entity.Parent = row[GPSTrackingDBV2.Entities.MenuColumn.Parent.ToString()] == System.DBNull.Value ? new int?() : Convert.ToInt32(row[GPSTrackingDBV2.Entities.MenuColumn.Parent.ToString()]);
                entity.ToolTip = Convert.ToString(row[GPSTrackingDBV2.Entities.MenuColumn.ToolTip.ToString()]);
                entity.Page = Convert.ToString(row[GPSTrackingDBV2.Entities.MenuColumn.Page.ToString()]);
                entity.IsVisibleMaster = Convert.ToBoolean(row[GPSTrackingDBV2.Entities.MenuColumn.IsVisibleMaster.ToString()]);
                entity.IsVisibleAdministrator = Convert.ToBoolean(row[GPSTrackingDBV2.Entities.MenuColumn.IsVisibleAdministrator.ToString()]);
                entity.IsVisibleUser = Convert.ToBoolean(row[GPSTrackingDBV2.Entities.MenuColumn.IsVisibleUser.ToString()]);
                entity.ImageUrl = Convert.ToString(row[GPSTrackingDBV2.Entities.MenuColumn.ImageUrl.ToString()]);
                entity.Width = row[GPSTrackingDBV2.Entities.MenuColumn.Width.ToString()] == System.DBNull.Value ? new short?() : Convert.ToInt16(row[GPSTrackingDBV2.Entities.MenuColumn.Width.ToString()]);
                entity.Heigth = row[GPSTrackingDBV2.Entities.MenuColumn.Heigth.ToString()] == System.DBNull.Value ? new short?() : Convert.ToInt16(row[GPSTrackingDBV2.Entities.MenuColumn.Heigth.ToString()]);
                entity.Positionx = row[GPSTrackingDBV2.Entities.MenuColumn.Positionx.ToString()] == System.DBNull.Value ? new short?() : Convert.ToInt16(row[GPSTrackingDBV2.Entities.MenuColumn.Positionx.ToString()]);
                entity.Positiony = row[GPSTrackingDBV2.Entities.MenuColumn.Positiony.ToString()] == System.DBNull.Value ? new short?() : Convert.ToInt16(row[GPSTrackingDBV2.Entities.MenuColumn.Positiony.ToString()]);
                entity.MenuTypeId = Convert.ToInt32(row[GPSTrackingDBV2.Entities.MenuColumn.MenuTypeId.ToString()]);
                entity.TypeModuleId = Convert.ToInt32(row[GPSTrackingDBV2.Entities.MenuColumn.TypeModuleId.ToString()]);
                entity.BySequence = Convert.ToInt16(row[GPSTrackingDBV2.Entities.MenuColumn.BySequence.ToString()]);

                entities.Add(entity);
            }

            return entities;
        }

        protected void MenuGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "RemoveRole")
            {
                Telerik.Web.UI.RadTreeView tree;
                if (HiddenFieldTreeViewHasFocus.Value == "Manager")
                    tree = MenuGrid.Items[0].FindControl("MenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                else
                {
                    if (HiddenFieldTreeViewHasFocus.Value == "Map")
                        tree = MenuGrid.Items[0].FindControl("MenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                    else
                        tree = MenuGrid.Items[0].FindControl("MenuRadMobileTreeView") as Telerik.Web.UI.RadTreeView;
                }
                

                foreach (RadTreeNode node in tree.SelectedNodes)
                {
                    try
                    {
                        int roleId = Convert.ToInt32(UserRolesGrid.Items[UserRolesGrid.SelectedItems[0].ItemIndex]["UserRolesID"].Text);
                        int menuId = Convert.ToInt32(tree.SelectedNode.Value);

                        GPSTrackingDBV2.Data.DataRepository.MenuByUserRolesProvider.Delete(roleId, menuId, getSessionManager().CompanyIDSelectedComboBox);

                        node.Remove();
                    }
                    catch
                    {

                    }
                    //tree.SelectedNode.Remove();
                }
            }
            if (e.CommandName == "ExpandAllNodes")
            {
                Telerik.Web.UI.RadTreeView tree;
                Telerik.Web.UI.RadTreeView tree1;
                tree = MenuGrid.Items[0].FindControl("MenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                tree1 = MenuGrid.Items[0].FindControl("MenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                tree.ExpandAllNodes();
                tree1.ExpandAllNodes();
            }
            if (e.CommandName == "CollapseAllNodes")
            {
                Telerik.Web.UI.RadTreeView tree;
                Telerik.Web.UI.RadTreeView tree1;
                tree = MenuGrid.Items[0].FindControl("MenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                tree1 = MenuGrid.Items[0].FindControl("MenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                tree.CollapseAllNodes();
                tree1.CollapseAllNodes();
            }
            ResizeGrid();
        }

        protected void AllMenuGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddRole")
            {
                Telerik.Web.UI.RadTreeView tree;
                if (HiddenFieldTreeViewHasFocus.Value == "AllManager")
                    tree = AllMenuGrid.Items[0].FindControl("AllMenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                else
                {
                    if (HiddenFieldTreeViewHasFocus.Value == "AllMap")
                        tree = AllMenuGrid.Items[0].FindControl("AllMenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                    else
                        tree = AllMenuGrid.Items[0].FindControl("AllMenuRadMobileTreeView") as Telerik.Web.UI.RadTreeView;

                }

                foreach(RadTreeNode node in tree.SelectedNodes){
                    try
                    {
                        insertMenuRadMapTreeView(Convert.ToInt32(node.Value));
                    }
                    catch { 
                    
                    }
                    //tree.SelectedNode.Remove();
                }
                Build_Menu(getRoleIdSelected());
            }
            if (e.CommandName == "ExpandAllNodes")
            {
                Telerik.Web.UI.RadTreeView tree;
                Telerik.Web.UI.RadTreeView tree1;
                tree = AllMenuGrid.Items[0].FindControl("AllMenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                tree1 = AllMenuGrid.Items[0].FindControl("AllMenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                tree.ExpandAllNodes();
                tree1.ExpandAllNodes();
            }
            if (e.CommandName == "CollapseAllNodes")
            {
                Telerik.Web.UI.RadTreeView tree;
                Telerik.Web.UI.RadTreeView tree1;
                tree = AllMenuGrid.Items[0].FindControl("AllMenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                tree1 = AllMenuGrid.Items[0].FindControl("AllMenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                tree.CollapseAllNodes();
                tree1.CollapseAllNodes();
            }
            ResizeGrid();
        }

        protected void AllMenuGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            AllMenuGrid.DataSource = new String[] { "Tree" };
        }

        private void BuildAllMenu()
        {
            Telerik.Web.UI.RadTreeView tree = AllMenuGrid.Items[0].FindControl("AllMenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;

            if (tree.Nodes.Count == 0)
            {
                GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.Menu> menuEntities = ConvertDataTableToEntities(GPSTrackingDBV2Custom.Data.DataRepository.CompaniesByMenuProvider.GetAllByCompanyID(getSessionManager().CompanyIDSelectedComboBox));
                //GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.Menu> menuEntities = GPSTrackingDBV2.Data.DataRepository.MenuProvider.GetAll();

                //Telerik.Web.UI.RadTreeView tree = AllMenuGrid.Items[0].FindControl("AllMenuRadManagerTreeView") as Telerik.Web.UI.RadTreeView;
                Telerik.Web.UI.RadTreeNode menuItemNivel1 = new Telerik.Web.UI.RadTreeNode();
                menuItemNivel1.Text = "Módulo Manager";
                tree.Nodes.Add(menuItemNivel1);

                var menuByUserRolesParentEntities = menuEntities.Where(x => x.Parent == null && x.MenuTypeId == 1 && x.IsVisibleUser).OrderBy(o => o.BySequence);

                foreach (GPSTrackingDBV2.Entities.Menu row in menuByUserRolesParentEntities)
                {
                    Telerik.Web.UI.RadTreeNode menuItem = new Telerik.Web.UI.RadTreeNode();
                    menuItem.ImageUrl = "../../Template/Icons/16px/" + row.ImageUrl;
                    menuItem.Text = row.Name;
                    menuItem.ToolTip = row.ToolTip;
                    menuItem.Value = row.MenuId.ToString();

                    var menuByUserRolesChildEntities = menuEntities.Where(x => x.Parent == row.MenuId && x.MenuTypeId == 1 && x.IsVisibleUser).OrderBy(o => o.BySequence);

                    foreach (GPSTrackingDBV2.Entities.Menu rowChild in menuByUserRolesChildEntities)
                    {
                        Telerik.Web.UI.RadTreeNode menuItemChild = new Telerik.Web.UI.RadTreeNode();
                        menuItemChild.ImageUrl = "../../Template/Icons/16px/" + rowChild.ImageUrl;
                        menuItemChild.Text = rowChild.Name;
                        menuItemChild.ToolTip = rowChild.ToolTip;
                        menuItemChild.Value = Convert.ToString(rowChild.MenuId);

                        menuItem.Nodes.Add(menuItemChild);
                    }

                    tree.Nodes[0].Nodes.Add(menuItem);
                }

                Telerik.Web.UI.RadTreeView tree2 = AllMenuGrid.Items[0].FindControl("AllMenuRadMapTreeView") as Telerik.Web.UI.RadTreeView;
                Telerik.Web.UI.RadTreeNode menuItemNivel2 = new Telerik.Web.UI.RadTreeNode();
                menuItemNivel2.Text = "Módulo Mapa";
                tree2.Nodes.Add(menuItemNivel2);


                var menuByUserRolesParentEntitiesMap = menuEntities.Where(x => x.Parent == null && x.MenuTypeId == 2 && x.IsVisibleUser).OrderBy(o => o.BySequence);
                foreach (GPSTrackingDBV2.Entities.Menu row in menuByUserRolesParentEntitiesMap)
                {
                    Telerik.Web.UI.RadTreeNode menuItem = new Telerik.Web.UI.RadTreeNode();
                    menuItem.ImageUrl = "../../Template/Icons/16px/" + row.ImageUrl;
                    menuItem.Text = row.Name;
                    menuItem.ToolTip = row.ToolTip;
                    menuItem.Value = row.MenuId.ToString();

                    var menuByUserRolesChildEntities = menuEntities.Where(x => x.Parent == row.MenuId && x.MenuTypeId == 2 && x.IsVisibleUser).OrderBy(o => o.BySequence);

                    foreach (GPSTrackingDBV2.Entities.Menu rowChild in menuByUserRolesChildEntities)
                    {
                        Telerik.Web.UI.RadTreeNode menuItemChild = new Telerik.Web.UI.RadTreeNode();
                        menuItemChild.ImageUrl = "../../Template/Icons/16px/" + rowChild.ImageUrl;
                        menuItemChild.Text = rowChild.Name;
                        menuItemChild.ToolTip = rowChild.ToolTip;
                        menuItemChild.Value = Convert.ToString(rowChild.MenuId);

                        menuItem.Nodes.Add(menuItemChild);
                    }

                    tree2.Nodes[0].Nodes.Add(menuItem);
                }

                Telerik.Web.UI.RadTreeView tree3 = AllMenuGrid.Items[0].FindControl("AllMenuRadMobileTreeView") as Telerik.Web.UI.RadTreeView;
                tree3.Nodes.Clear();
                Telerik.Web.UI.RadTreeNode menuItemNivel3 = new Telerik.Web.UI.RadTreeNode();
                menuItemNivel3.Text = "Módulo Dispositivos Móviles";
                tree3.Nodes.Add(menuItemNivel3);



                var menuByUserRolesParentEntitiesMobile = menuEntities.Where(x => x.Parent == null && x.MenuTypeId == 3).OrderBy(o => o.BySequence);
                foreach (GPSTrackingDBV2.Entities.Menu row in menuByUserRolesParentEntitiesMobile)
                {
                    Telerik.Web.UI.RadTreeNode menuItem = new Telerik.Web.UI.RadTreeNode();
                    menuItem.ImageUrl = "../../Template/Icons/16px/pda-icon.png";
                    menuItem.Text = row.Name;
                    menuItem.ToolTip = row.ToolTip;
                    menuItem.Value = row.MenuId.ToString();

                    tree3.Nodes[0].Nodes.Add(menuItem);
                }

                tree.ExpandAllNodes();
                tree2.ExpandAllNodes();
                tree3.ExpandAllNodes();
            }
        }
        
        protected void deleteMenuRadMapTreeView(int menuId)
        {
            int roleId = getRoleIdSelected();      
            GPSTrackingDBV2.Data.DataRepository.MenuByUserRolesProvider.Delete(roleId, menuId, getSessionManager().CompanyIDSelectedComboBox);
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void insertMenuRadMapTreeView(int menuId)
        {
            
            GPSTrackingDBV2.Entities.MenuByUserRoles menuByUserRolesEntity = new GPSTrackingDBV2.Entities.MenuByUserRoles();
            menuByUserRolesEntity.UserRoleId = getRoleIdSelected();
            menuByUserRolesEntity.MenuId = menuId;
            menuByUserRolesEntity.CompanyId = getSessionManager().CompanyIDSelectedComboBox;
            GPSTrackingDBV2.Data.DataRepository.MenuByUserRolesProvider.Insert(menuByUserRolesEntity);
        }

        protected void AllMenuRadManagerTreeView_DataBound(object sender, EventArgs e)
        {
            BuildAllMenu();
        }

        protected void UserRolesGrid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.UserRoles UserRolesEntity = new GPSTrackingDBV2.Entities.UserRoles();

                UserRolesEntity.UserRolesId = GPSTrackingDBV2Custom.Data.DataRepository.UserRolesProvider.GetNextID(getSessionManager().CompanyIDSelectedComboBox);
                UserRolesEntity.Name = Entity["Name"].ToString();
                UserRolesEntity.CompanyId = getSessionManager().CompanyIDSelectedComboBox;

                GPSTrackingDBV2.Data.DataRepository.UserRolesProvider.Insert(UserRolesEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                ShowErrorMessage("No se puede insertar la información.");
            }
        }

        protected void UserRolesGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.UserRoles UserRolesEntity = new GPSTrackingDBV2.Entities.UserRoles();

                UserRolesEntity.UserRolesId = int.Parse(Entity["UserRolesID"].ToString());
                UserRolesEntity.OriginalUserRolesId = int.Parse(Entity["UserRolesID"].ToString());
                UserRolesEntity.CompanyId = getSessionManager().CompanyIDSelectedComboBox;
                UserRolesEntity.OriginalCompanyId = getSessionManager().CompanyIDSelectedComboBox;
                UserRolesEntity.Name = Entity["Name"].ToString();

                GPSTrackingDBV2.Data.DataRepository.UserRolesProvider.Update(UserRolesEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                ShowErrorMessage("No se puede actualizar la información.");
            }
        }

        protected void UserRolesGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Data.DataRepository.UserRolesProvider.Delete(getSessionManager().CompanyIDSelectedComboBox, int.Parse(Entity["UserRolesID"].ToString()));

            }
            catch (Exception ex)
            {
                ShowErrorMessage("No se pudo quitar el rol del usuario seleccionado.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
            
        }

    }
}